home *** CD-ROM | disk | FTP | other *** search
/ Computer Inter@ctive 17 / Computer Interactive cdrom 17 - gen 99.iso / ZDNETIT / CONTENT / OPTIVDOS.ZIP / DOC.ZIP / VDEMO.CPP < prev   
Encoding:
C/C++ Source or Header  |  1998-10-21  |  4.8 KB  |  121 lines

  1. /*******************************   VDEMO.CPP *****************************
  2. *                                                                        *
  3. *                    Sample program for the demonstration                *
  4. *                    of VectorLib for Borland C/C++                      *
  5. *                    for DOS                                             *
  6. *                                                                        *
  7. *       Copyright 1993-1998 by Martin Sander                             *
  8. *                                                                        *
  9. **************************************************************************/
  10. #define BGIDIR "..\\BGI\\"  // this must be the path to the BGI directory!
  11. #include <VEstd.h>
  12. #include <VEmath.h>
  13. #include <VCEstd.h>
  14. #include <VCEmath.h>
  15. #include <Vgraph.h>
  16. #include <math.h>
  17. #include <DOS.h>
  18. #include <conio.h>
  19. #include <stdlib.h>
  20.  
  21. struct time    t1, t2;
  22. void starttime( void )
  23. {
  24.    gettime( &t1 );
  25. }
  26.  
  27. double stoptime( void )
  28. {
  29.    double         tBegin, tEnd;
  30.    gettime( &t2 );
  31.    tBegin = t1.ti_min * 60L + t1.ti_sec + t1.ti_hund * 0.01;
  32.    tEnd   = t2.ti_min * 60L + t2.ti_sec + t2.ti_hund * 0.01;
  33.  
  34.    return( tEnd - tBegin );
  35. }
  36.  
  37. extern unsigned _ovrbuffer = 0x0800;
  38. void main( void )
  39. {
  40.    ui             xsize=1024, specsiz=128, csize = 200, i, j;
  41.    eVector        X, Y, Win, Spc, Time, Freq;
  42.    extended       a, b, c;
  43.    ceVector       CX, CY;
  44.  
  45.    X = VE_vector( xsize );
  46.    Y = VE_vector( xsize );
  47.    printf( "\n\nThis is a short demonstration of VectorLib for C/C++"
  48.             "\nin DOS real mode."
  49.            "\nHit any key to continue  or abort with [ESC] !" );
  50.    if( getch() == 27 ) exit(0);
  51.  
  52.    Spc  = VE_vector( specsiz+1);
  53.    Win  = VE_vector( 2*specsiz );
  54.    Time = VE_vector( xsize );
  55.    Freq = VE_vector( specsiz+1 );
  56.  
  57.    VE_ramp( Time, xsize, 0, 80*M_PI/xsize );
  58.    VE_ramp( Freq, specsiz+1, 0, (xsize / (80*M_PI)) / specsiz );
  59.             /* get a time axis from 0 to 80 Pi and a corresponding
  60.                frequency axis from 0 to the Nyquist frequency */
  61.    VE_sin( X, Time, xsize );
  62.    VE_cmpC( X, X, xsize, 0.7 ); /* get an asymmetric square wave */
  63.                /* instead of the last two lines, try also, e.g.:
  64.                   VE_noise( X, xsize, 1, 1 );
  65.                   VE_smooth( X, X, xsize, 3 );  */
  66.    V_initGraph( BGIDIR );  /* this must be your correct path! */
  67.  
  68.    VE_Welch( Win, 2*specsiz );
  69.                /* You might as well take VE_Parzen or VE_Hanning  */
  70.    Spc[specsiz] = VE_spectrum( Spc, specsiz, X, xsize, Win );
  71.  
  72.    VE_xyAutoPlot( Time, X, 101, PS_SOLID, GREEN );
  73.    printf( "This is a\nPortion of\nan asymmetric\nsquare wave" );
  74.    if( getch() == 27 ) exit(0);
  75.    VE_xyAutoPlot( Freq, Spc, specsiz+1, PS_SOLID | SY_CROSS, GREEN );
  76.    printf( "Power Density\nSpectrum of\nthe same\nsquare wave" );
  77.    if( getch() == 27 ) exit(0);
  78.    V_nfree( 4, Spc, Win, Time, Freq );
  79.  
  80.    /* let's have a look at complex arithmetics  */
  81.    CX = VCE_vector( csize );
  82.    CY = VCE_vector( csize );
  83.    VCE_ramp( CX, csize, ecplx(M_PI, 0), ecplx(0.2, 0.0005) );
  84.    VCE_sin( CY, CX, csize );
  85.    VCE_cos( CX, CX, csize );
  86.         /*  Try also with other complex functions! */
  87.  
  88.    VCE_2AutoPlot( CY, csize, PS_SOLID, GREEN,
  89.                   CX, csize, PS_SOLID, RED );
  90.    printf( "Playing with\nfunctions in\nthe complex plane:\n\nGreen: sine,\nRed:   cosine." ); getch();
  91.    V_nfree( 2, CX, CY );
  92.    closegraph();
  93.  
  94.    VE_ramp( X, xsize, -10, 0.020 );
  95.    a = 1.1;  b = -0.15;  c = 0.02;   /* or any other arbitrary values... */
  96.    printf( "\nTo end this short program, let us make a crude speed comparison"
  97.            "\nbetween compiled code and VectorLib functions."
  98.            "\nE.g., evaluate the exponential function for a whole vector"
  99.            "\n\n          y[i] = c * exp( a * x[i] + b ),     i=0,..,size-1"
  100.            "\n\n(10 runs of 1024 elements each).      Please wait...\n" );
  101.    starttime();
  102.    for( i=0; i<100; i++ )   /* repeat enough times to see the difference */
  103.       for( j=0; j<xsize; j++ )  Y[j] = c * exp( a * X[j] + b );
  104.    printf( "\nExecution Time compiled code:      %lf sec", stoptime() );
  105.       /* now the VectorLib solution to the same simple problem:  */
  106.    starttime();
  107.    for( i=0; i<100; i++ )
  108.       VEx_exp( Y, X, xsize, a, b, c );
  109.    printf( "\nExecution Time VectorLib function: %lf sec", stoptime() );
  110.  
  111.    printf( "\n\nNormally, the VectorLib function is about two times faster"
  112.             "\nthan compiled code. In some environments, up to a factor of 10"
  113.          "\nmay be gained."
  114.          "\n\nChange the source code of this example program and try also"
  115.             "\nother mathematical functions, like the sine, the hyperbolic"
  116.         "\nsine, and so on..." );
  117.             getch();
  118.    V_nfree( 2, X, Y );
  119. }
  120.  
  121.